package ac.neec.mio.record.search;

import java.util.Calendar;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.SlidingDrawer;
import android.widget.TextView;

/**
 * Created by G011C1093 on 14/01/24.
 */
public class SearchSlidingDrawer extends SlidingDrawer
						implements SlidingDrawer.OnDrawerCloseListener{

    private static final String DATE_UI = "/";

    private static int nYear;
    private static int nMonth;
    private static int nDay;
    private TextView startDate;
    private TextView endDate;

    public int getnYear() {
        return nYear;
    }

    public int getnMonth() {
        return nMonth;
    }

    public int getnDay() {
        return nDay;
    }

    public static String getDateToday(){
    	StringBuilder sb = new StringBuilder();

    	sb.append(nYear);
    	sb.append(DATE_UI);
    	if(nMonth < 10)
    		sb.append(0);
    	sb.append(nMonth);
    	sb.append(DATE_UI);
    	sb.append(nDay);
    	return String.valueOf(sb);
    }

    public SearchSlidingDrawer(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public SearchSlidingDrawer(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        setOnDrawerCloseListener(this);
        setFocusable(true);
        setFocusableInTouchMode(true);
    }

    private void initDate() {
        Calendar cal = Calendar.getInstance();
        nYear = cal.get(Calendar.YEAR);
        nMonth = cal.get(Calendar.MONTH) + 1;
        nDay = cal.get(Calendar.DAY_OF_MONTH);
        startDate.setText(setDateToString(nYear, nMonth, nDay));
        endDate.setText(setDateToString(nYear, nMonth, nDay));
        InputDataChecker.setSrartDate(nYear, nMonth, nDay);
        InputDataChecker.setEndDate(nYear, nMonth, nDay);
    }

    public void setDateText(TextView startDate, TextView endDate) {
        this.startDate = startDate;
        this.endDate = endDate;
        initDate();
    }

    public void setStartDate(int y, int m, int d) {
        if (InputDataChecker.checkStartDate(y, m, d)) {
            startDate.setText(setDateToString(y, m, d));
            InputDataChecker.setSrartDate(y, m, d);
        } else {
            startDate.setText(setDateToString(y, m, d));
            endDate.setText(setDateToString(y, m, d));
            InputDataChecker.setEndDate(InputDataChecker.geteYear(),
                    InputDataChecker.geteMonth(), InputDataChecker.geteDay());
        }
    }

    public void setEndDate(int y, int m, int d) {
        if (InputDataChecker.checkEndDate(y, m, d)) {
            endDate.setText(setDateToString(y, m, d));
            InputDataChecker.setEndDate(y, m, d);
        } else {
            startDate.setText(setDateToString(y, m, d));
            endDate.setText(setDateToString(y, m, d));
            InputDataChecker.setSrartDate(InputDataChecker.getsYear(),
                    InputDataChecker.getsMonth(), InputDataChecker.getsDay());
        }
    }


    private String setDateToString(int y, int m, int d) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(y);
        stringBuilder.append(DATE_UI);
        if(m<10)
        	stringBuilder.append(0);
        stringBuilder.append(m);
        stringBuilder.append(DATE_UI);
        if(d<10)
        	stringBuilder.append(0);
        stringBuilder.append(d);
        return String.valueOf(stringBuilder);
    }


    @Override
    public void onDrawerClosed() {
        // 再レイアウト
        invalidate();
        requestLayout();
    }

}
